package com.reesechou.controller.admin;

import com.reesechou.javabean.Comment;
import com.reesechou.service.CommentService;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created by 小星打屁屁 on 2015/4/20.
 * contact me ==>243709758@qq.com
 */
@Controller
@RequestMapping("/jsp/admin/content")
public class CommentController {
    private static  Logger logger = Logger.getLogger(CommentController.class);
    @Autowired
    private CommentService commentService ;

    private static String COMMENT_ADD = "comment_add";

    private static String COMMNET_LIST = "comment_list";

    private static String COMMENT_RESPONSE = "comment_response";

    @InitBinder
   private void initBinder(WebDataBinder webDataBinder){
        //The date format to parse or output your dates
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//将要转化成的格式

        CustomDateEditor dateEditor = new CustomDateEditor(dateFormat,true);//生成一个customDateEditor 转换的值可以为空

        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }

    //获取所有的评论并组成列表返回显示
    @RequestMapping(value="/comment_list")
    public String control(Model model,Comment comment){

        List<Comment> commentList = commentService.findAllComment();
        model.addAttribute("comments",commentList);

        return COMMNET_LIST;
    }

    @RequestMapping(value="/comment_add",method= RequestMethod.GET)
    public String createMethodGet(Model model ,Comment comment){

        return COMMENT_ADD;
    }

    @RequestMapping(value="/comment_add",method=RequestMethod.POST)
    public String createMethodPost(Model model ,Comment comment){

        createDataPrepare(model,comment);
        if(commentService.insert(comment)>=1) System.out.println("评论添加成功了");
        else System.out.println("评论添加失败了");
        return "redirect:"+COMMENT_ADD ;
    }

    public void createDataPrepare(Model model,Comment comment){
        //TODO: 这里还要记得处理客户端的 ip 地址问题
        Date postTime = new Date();
        String postIP = null;
        String status = null;

        System.out.println("  name = "+ comment.getName());


    }
}
